package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorService dishFlavorService;

    //扩展方法：保存菜品，菜品口味数据
    @Override
    @Transactional
    public void saveDishWithFlavor(DishDto dishDto) {
        //1. 添加菜品数据，往 dish 表中添加一条数据
        this.save(dishDto); //只有保存完dish之后，id才会有值

        //2. 添加口味数据，往 dish_flavor 表中添加多条数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //TODO 给每个口味设置菜品id
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        //批量保存
        dishFlavorService.saveBatch(flavors);
    }

    //扩展方法：分页查询菜品数据，包括分类名称
    @Override
    public Page<DishDto> pageDishDto(Integer page, Integer pageSize, String name) {
        //创建分页对象
        Page<DishDto> pageInfo = new Page<>(page, pageSize);
        //调用mapper分页查询
        return dishMapper.pageDishDto(pageInfo, name);
    }

    //扩展方法：批量删除菜品，以及菜品口味数据，如果有菜品状态为启售，则不做操作
    @Override
    @Transactional
    public void deleteByIds(Long[] ids) {
        //1. 根据菜品ids，查询其中是否有菜品为启售
        // select count(*) from dish where id in (ids) and status=1
        Integer count = this.lambdaQuery()
                .in(Dish::getId, ids)
                .eq(Dish::getStatus, 1)
                .count();
        if (count > 0) {
            throw new CustomerException("有菜品正在售卖，不能删除！");
        }

        //2. 删除菜品信息
        // delete from dish where id in (ids)
        this.removeByIds(Arrays.asList(ids));

        //3. 删除菜品口味信息
        // delete from dish_flavor where dish_id in (ids)
        dishFlavorService.lambdaUpdate()
                .in(DishFlavor::getDishId, ids)
                .remove();
    }

    //扩展方法：根据id查询菜品详情信息（包含口味数据）
    @Override
    public DishDto getDishByIdWithFlavor(Long id) {
        DishDto dishDto = dishMapper.getDishByIdWithFlavor(id);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateDishWithFlavor(DishDto dishDto) {
        //1. 修改菜品dish表数据
        this.updateById(dishDto);

        //2. 修改菜品口味数据（先删再加）
        // delete from dish_flavor where dish_id=?
        dishFlavorService.lambdaUpdate()
                .eq(DishFlavor::getDishId, dishDto.getId())
                .remove();
        List<DishFlavor> flavors = dishDto.getFlavors();
        //TODO 给每个口味设置菜品id
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        //批量保存
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 自定义扩展方法:条件查询菜品信息,包含口味
     *
     * @param categoryId
     * @param name
     * @return
     */
    @Override
    public List<DishDto> listDishDto(Long categoryId, String name) {
        return dishMapper.listDishDto(categoryId, name);
    }

    /**
     * 根据套餐id查询对应菜品信息
     *
     * @param setmealId
     * @return
     */
    @Override
    public List<DishDto> findById(Long setmealId) {
        return dishMapper.findById(setmealId);
    }
}


